package com.ck.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ck.client.model.vo.FileMonitorVO;
import com.ck.client.service.MonitService;
import com.ck.core.constant.ElasticsearchConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 终端监播上报serviceImpl
 * @author: zhanglei
 * @date: 2019/4/23 14:18
 */
@Service(value = "monitService")
public class MonitServiceImpl implements MonitService {
    private static final String PROGRAM_VERSION = "programVersion";
    private static final String DEVICE_CODE = "deviceCode";
    private static Logger fileLogger = LoggerFactory.getLogger("monitorFileData");
    private static Logger programLogger = LoggerFactory.getLogger("monitorProgramData");

    @Override
    public void reportShows(String deviceCode, String jsonData) {
        //转成json格式
        JSONObject jsonObject = JSON.parseObject(jsonData);
        //解析监播数据外层信息
        String programVersion = jsonObject.getString(PROGRAM_VERSION);
        //String deviceCode = jsonObject.getString(DEVICE_CODE);
        Long programPlayStartTime = jsonObject.getLong("programPlayStartTime");
        //节目播放时长
        Long programDuration = 0L;
        //解析监播数据素材信息
        JSONArray fileList = jsonObject.getJSONArray("fileList");
        String fileListStr = JSONObject.toJSONString(fileList);
        List<FileMonitorVO> fileMonitorVOList = JSONObject.parseArray(fileListStr,FileMonitorVO.class);
        String interval = ElasticsearchConstant.SEPARATOR;
        if(!fileMonitorVOList.isEmpty()){
            for(FileMonitorVO vo:fileMonitorVOList){
                String fileName = vo.getFileName();
                String fileURL = vo.getFileURL();
                Long filePlayStartTime = vo.getFilePlayStartTime();
                Long fileDuration = vo.getFileDuration();
             //   String programVersionDeviceCode = programVersion+interval+deviceCode;
                String fileNameURL = fileName+interval+fileURL;
                //累加广告时长
                programDuration+=fileDuration;
                //将素材信息写进素材监播日志
                JSONObject fileJsonObject = new JSONObject();
                //fileJsonObject.put("programVersionDeviceCode",programVersionDeviceCode);
                fileJsonObject.put(PROGRAM_VERSION,programVersion);
                fileJsonObject.put(DEVICE_CODE,deviceCode);
                fileJsonObject.put("fileNameURL",fileNameURL);
                fileJsonObject.put("playDuration",fileDuration);
                fileJsonObject.put("filePlayStartTime",filePlayStartTime);
                String fileMsg = fileJsonObject.toJSONString();
                //log
                fileLogger.info(fileMsg);
            }
        }
        //将节目信息写进节目监播日志
        JSONObject programJsonObject = new JSONObject();
        programJsonObject.put(PROGRAM_VERSION,programVersion);
        programJsonObject.put(DEVICE_CODE,deviceCode);
        programJsonObject.put("programPlayStartTime",programPlayStartTime);
        programJsonObject.put("playDuration",programDuration);
        String programMsg = programJsonObject.toJSONString();
        //log
        programLogger.info(programMsg);
    }

}
